<template>
  <div>
    <el-form ref="deptForm" :rules="rules" label-width="120px" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- <el-option value="1" :label="小美丽" /> -->
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      require: true
    },
    isEdit: {
      type: Boolean,
      require: true
    },
    originList: {
      type: Array,
      require: true
    }
  },
  data() {
    // 自定义校验的回调函数
    const validCode = (rule, value, cellback) => {
      // value 当前校验的值
      // 如果检验通过 cellback()
      // 如果不通过 cellback(new Error('错误说明'))
      let originCodes = this.originList.map(item => item.code)

      if (this.isEdit) {
        originCodes = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }

      console.log('要校验的值', value, originCodes)
      // 判断当前添加的编码是否存在于数组中
      if (originCodes.includes(value)) {
        cellback(new Error(value + '已存在'))
      } else {
        cellback()
      }
    }

    // 判断同级名称不重复
    const validName = (rule, value, cellback) => {
      let originName = this.originList.filter(item => item.pid === this.id).map(item => item.name)

      if (this.isEdit) {
        const cur = this.originList.find(item => item.id === this.id)
        const pid = cur.pid
        const brothers = this.originList.filter(item => item.pid === pid && item.id !== this.id)
        originName = brothers.map(item => item.name)
        console.log('id', cur)
      }

      console.log(value, originName)

      // 判断当前的名称是否在已存在的数组中
      if (originName.includes(value)) {
        cellback(new Error(value + '已存在'))
      } else {
        cellback()
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          // 自定义检验规则 validdator
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      employees: []
    }
  },
  created() {
    this.loadEmployee()

    if (this.isEdit) {
      this.loadDetail()
    }
  },
  methods: {
    async loadDetail() {
      const res = await getDepartDetail(this.id)
      this.form = res.data
      console.log(res, 2)
    },
    async loadEmployee() {
      const res = await getEmployeeSimple()
      this.employees = res.data
      console.log(res, 666)
    },
    async doAdd() {
      const d = { ...this.form, pid: this.id }
      console.log(d)
      // 新增部门
      await addDepartments(d)

      // 子传父
      this.$emit('success')
    },
    async doEdit() {
      // 编辑并保存数据
      await updateDepartments(this.form)

      // 子传父
      this.$emit('success')
    },
    // 点击确认按钮
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 点击取消按钮
    hCancel() {
      this.$emit('success')
    }
  }
}
</script>
